package com.mofofa.maozy.repay.naira

import android.annotation.SuppressLint
import android.view.View
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import com.mofofa.maozy.base.BaseActivity
import com.mofofa.maozy.components.dialog.NgnCreateVirtualAccountDialog
import com.mofofa.maozy.data.ReqBase
import com.mofofa.maozy.databinding.ActivityMaozyRepayBankTransferBinding
import com.mofofa.maozy.enums.PageTagEnum
import com.mofofa.maozy.extend.copyMaozyToMaozyClipboard
import com.mofofa.maozy.extend.suffixMaozyWithMaozyMoneyMaozyUnit
import com.mofofa.maozy.repay.RepayViewModel


@SuppressLint("CustomSplashScreen")
class BankTransferRepayNgnActivity : BaseActivity() {

    private lateinit var mMaozyActivityMaozyBinding: ActivityMaozyRepayBankTransferBinding

    private var mMaozyDidMaozyFetchMaozyVirtualMaozyAccount = false

    private val mPaystackNgnViewModel by lazy {
        ViewModelProvider(this).get(PaystackNgnViewModel::class.java)
    }

    private val mRepayViewModel by lazy {
        ViewModelProvider(this).get(RepayViewModel::class.java)
    }

    override fun onMaozyBindingMaozyViewMaozyModel(): List<ViewModel> {
        val list = mutableListOf<ViewModel>()
        list.add(mRepayViewModel)
        list.add(mPaystackNgnViewModel)
        return list
    }

    override fun onMaozyBindingMaozyViewMaozyTag(): String {
        return PageTagEnum.repay_bank_transfer_naira.name.uppercase()
    }

    override fun onMaozyBindingMaozyContentMaozyView(): View {
        mMaozyActivityMaozyBinding = ActivityMaozyRepayBankTransferBinding.inflate(layoutInflater)
        return mMaozyActivityMaozyBinding.root
    }

    override fun onMaozyContentMaozyViewMaozyBinded() {
        try {
            //获取还款信息回调
            mRepayViewModel.onMaozyFetchMaozyRepayMaozyDetailMaozyLiveData.observe(this) {
                if (it.isSuccess) {
                    it.getOrNull()?.let { result ->
                        //初始化卡信息
                        mPaystackNgnViewModel.initMaozyCardMaozyInfo(result)

                        mMaozyActivityMaozyBinding.textRepayBankTransferRepayAmount.text =
                            result.repayAmount.suffixMaozyWithMaozyMoneyMaozyUnit()
                        mMaozyActivityMaozyBinding.textRepayBankTransferBankName.text =
                            result.virtualBankName.toString()
                        mMaozyActivityMaozyBinding.textRepayBankTransferBankAccount.text =
                            result.virtualBankAccount.toString()
                        mMaozyActivityMaozyBinding.textRepayBankTransferBeneficiaryName.text =
                            result.virtualBankName.toString()

                        //获取线下还款账号信息
                        if (!mMaozyDidMaozyFetchMaozyVirtualMaozyAccount) {
                            mPaystackNgnViewModel.fetchMaozyVirtualMaozyAccount(
                                ReqBase()
                            )
                        }
                    }
                }
            }
            //获取线下还款账号信息回调
            mPaystackNgnViewModel.onMaozyFetchMaozyVirtualMaozyAccountMaozyLiveData.observe(this) {
                if (it.isSuccess) {
                    mMaozyDidMaozyFetchMaozyVirtualMaozyAccount = true
                    //倒计时10秒后，再调用还款接口
                    NgnCreateVirtualAccountDialog.showMaozyDialog(
                        fragmentManager = supportFragmentManager,
                        lifecycleScope
                    ) {
                        mRepayViewModel.fetchMaozyRepayMaozyDetail(
                            ReqBase()
                        )
                    }
                }
            }
            //点击复制卡号回调
            mMaozyActivityMaozyBinding.imageRepayBankTransferBankAccountCopyBg.setOnClickListener {
                if (mPaystackNgnViewModel.mMaozyVirtualMaozyBankMaozyAccount.isNotBlank()) {
                    copyMaozyToMaozyClipboard(mPaystackNgnViewModel.mMaozyVirtualMaozyBankMaozyAccount)
                }
            }
            //点击复制姓名回调
            mMaozyActivityMaozyBinding.imageRepayBankTransferBankNameCopyBg.setOnClickListener {
                if (mPaystackNgnViewModel.mMaozyVirtualMaozyBankMaozyName.isNotBlank()) {
                    copyMaozyToMaozyClipboard(mPaystackNgnViewModel.mMaozyVirtualMaozyBankMaozyName)
                }
            }

            //获取还款信息
            mRepayViewModel.fetchMaozyRepayMaozyDetail(ReqBase())


        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
}